/**
 * Copyright (c) 2015-2017, Henry Yang 杨勇 (gismail@foxmail.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lambkit.module.upms.model;

import com.jfinal.kit.StrKit;
import com.lambkit.Lambkit;
import com.lambkit.common.model.LambkitModel;
import com.lambkit.db.sql.column.Column;
import com.lambkit.module.upms.UpmsConfig;
import com.lambkit.module.upms.UpmsManager;
import com.lambkit.module.upms.record.UpmsAuthCode;
import com.lambkit.module.upms.row.IUpmsAuthCode;
import com.lambkit.module.upms.sql.UpmsAuthCodeSqlBuilder;
import com.lambkit.module.upms.server.service.*;
import com.lambkit.module.upms.server.service.impl.UpmsAuthCodeModelServiceImpl;

/**
 * 授权码，目前已用于：
 * 1：账号激活
 * 2：密码找回
 * 未来随着业务增加可添加新类型，可能需要加 data 字段传递业务所需的额外数据
 * @author yangyong 
 * @website: www.lambkit.com
 * @email: gismail@foxmail.com
 * @date 2020-08-14
 * @version 1.0
 * @since 1.0
 */
public class UpmsAuthCodeModel extends LambkitModel<UpmsAuthCodeModel> implements IUpmsAuthCode<UpmsAuthCodeModel>  {
	
	private static final long serialVersionUID = 1L;
	
	public static UpmsAuthCodeModelService service() {
		return Lambkit.get(UpmsAuthCodeModelService.class, UpmsAuthCodeModelServiceImpl.class);
	}
	
	public static UpmsAuthCodeSqlBuilder sql() {
		return new UpmsAuthCodeSqlBuilder();
	}
	
	public static UpmsAuthCodeSqlBuilder sql(Column column) {
		UpmsAuthCodeSqlBuilder that = new UpmsAuthCodeSqlBuilder();
		that.add(column);
        return that;
    }

	public static UpmsAuthCodeModel create(UpmsAuthCode bean) {
		UpmsAuthCodeModel model = new UpmsAuthCodeModel();
		model._setOrPut(bean.getColumns());
		return model;
	}

	public UpmsAuthCodeModel() {
		UpmsConfig config = UpmsManager.me().getConfig();
		String dbconfig = config.getDbconfig();
		if(StrKit.notBlank(dbconfig)) {
			this.use(dbconfig);
		}
	}

	public UpmsAuthCode toPojo() {
		UpmsAuthCode bean = new UpmsAuthCode();
		bean.setColumns(this);
		return bean;
	}
	
	public static final int TYPE_REG_ACTIVATE = 0;			// 注册激活
	public static final int TYPE_RETRIEVE_PASSWORD = 1;		// 找回密码

	/**
	 * 在保存前保障 type 正确，随着 type 的增加，需要修改此处的代码
	 */
	public boolean save() {
		int type = getType();
		if (type < TYPE_REG_ACTIVATE || type > TYPE_RETRIEVE_PASSWORD) {
			throw new RuntimeException("授权码类型不正确: " + type);
		}
		return super.save();
	}

	public boolean update() {
		throw new RuntimeException("授权码不支持更新操作");
	}

	/**
	 * 是否是有效的注册激活授权码
	 */
	public boolean isValidRegActivateAuthCode() {
		return notExpired() && isTypeRegActivate();
	}

	/**
	 * 是否是有效的密码找回授权码
	 */
	public boolean isValidRetrievePasswordAuthCode() {
		return notExpired() && isTypeRetrievePassword();
	}

	/**
	 * 是否是账号激活授权码
	 */
	public boolean isTypeRegActivate() {
		return getType() == TYPE_REG_ACTIVATE;
	}

	/**
	 * 是否是密码找回授权码
	 */
	public boolean isTypeRetrievePassword() {
		return getType() == TYPE_RETRIEVE_PASSWORD;
	}

	/**
	 * 是否已过期
	 */
	public boolean isExpired() {
		return getExpireAt() < System.currentTimeMillis();
	}

	/**
	 * 是否未过期
	 */
	public boolean notExpired() {
		return ! isExpired();
	}
}
